<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('打票时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('数字彩票票号')">
              <a-input :placeholder="$t('请输入数字彩票票号')" v-model="queryParam.lotteryNo"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('业务类型')">
                <j-dict-select-tag
                  :placeholder="$t('请选择业务类型')"
                  v-model="queryParam.bizType"
                  dictCode="bis_member_lottery.biz_type"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('票类型')">
                <j-dict-select-tag
                  :placeholder="$t('请选择票类型')"
                  v-model="queryParam.type"
                  dictCode="bis_member_lottery.type"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('票状态')">
                <a-select v-model="queryParam.state" :placeholder="$t('请选择票状态')">
                  <a-select-option v-for="(item, index) in stateList" :key="index" :value="item.value">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('所属项目')">
                <j-dict-select-tag
                  :placeholder="$t('请选择所属项目')"
                  v-model="queryParam.gameId"
                  dictCode="bis_game,name,id"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('设备名称(编码)')">
                <a-select show-search :filter-option="false" @search="handleSearch" v-model="queryParam.deviceId">
                  <a-select-option :value="item.id" :key="index" v-for="(item, index) in deviceList">
                    <span>{{ item.name }}</span>
                    <span v-if="item.name != $t('全部')">({{ item.id }})</span>
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('业务渠道')">
                <j-dict-select-tag
                  :placeholder="$t('请选择业务渠道')"
                  v-model="queryParam.deviceType"
                  dictCode="bis_device_info.type"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('会员手机号')">
                <a-input :placeholder="$t('请输入手机号')" v-model="queryParam.memberPhone"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('卡号')">
                <a-input :placeholder="$t('请输入卡号')" v-model="queryParam.chipNo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('姓名')">
                <a-input :placeholder="$t('请输入姓名')" v-model="queryParam.memberName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('机号')">
                <a-input :placeholder="$t('请输入机号')" v-model="queryParam.machineNo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
              <a-form-item :label="$t('门店')">
                <a-select v-model="queryParam.tenantId">
                  <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value">
                    {{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" :loading="exloading" @click="handleExportXls($t('数字彩票信息'))" v-has="'BisMemberLotteryController:export'">{{
        $t('导出')
      }}</a-button>
      <a-button type="primary" @click="setCheck">{{ $t('验票设置') }}</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleFill(record)" v-has="'BisMemberLotteryController:lateplay'">{{ $t('补打') }}</a>
          <a-popconfirm v-has="'BisMemberLotteryController:edit'" :title="$t('您确定作废该彩票吗?')" @confirm="() => handleCancel(record)">
            <a style="margin-left: 8px" v-if="record.state != '4'">{{ $t('作废') }}</a>
          </a-popconfirm>
        </span>
        <template slot="footer">
          <div style="font-weight: bold">
            <span>{{ $t('合计') }}：{{ totalInfo.value }}</span>
          </div>
        </template>
      </a-table>
    </div>
    <fill-modal ref="fillModal"></fill-modal>
    <check-lottery-modal ref="checkModal"></check-lottery-modal>
  </a-card>
</template>

<script>

import Vue from 'vue'
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { putAction, getAction } from '@/api/manage'
import FillModal from './modules/FillModal'
import CheckLotteryModal from './modules/CheckLotteryModal'

export default {
  name: 'BisMemberLotteryList',
  mixins: [JeecgListMixin, mixinDevice],
  components: { FillModal, CheckLotteryModal },
  data() {
    return {
      description: '数字彩票信息管理页面',
      moment,
      queryParam: {
        state: '0',
        lotteryType: 1,
      },
      dataSource: [],
      // 表头
      columns: [
        {
          title: this.$t('时间'),
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: this.$t('业务类型'),
          align: 'center',
          dataIndex: 'bizType_dictText',
        },
        {
          title: this.$t('数字彩票票号'),
          align: 'center',
          dataIndex: 'lotteryNo',
        },
        {
          title: this.$t('类型'),
          align: 'center',
          dataIndex: 'type_dictText',
        },
        {
          title: this.$t('票值'),
          align: 'center',
          dataIndex: 'value',
          customRender: (text, r) => {
            return r.value.toFixed(0);
          }
        },
        {
          title: this.$t('状态'),
          align: 'center',
          dataIndex: 'state_dictText',
        },
        {
          title: this.$t('所属项目'),
          align: 'center',
          dataIndex: 'gameId_dictText',
        },
        {
          title: this.$t('设备编码'),
          align: 'center',
          dataIndex: 'deviceId',
        },
        {
          title: this.$t('机号'),
          align: 'center',
          dataIndex: 'machineNo',
        },
        {
          title: this.$t('设备名称'),
          align: 'center',
          dataIndex: 'deviceName',
        },
        {
          title: this.$t('业务渠道'),
          align: 'center',
          dataIndex: 'deviceType_dictText',
        },
        {
          title: this.$t('手机号'),
          align: 'center',
          dataIndex: 'memberPhone',
        },
        {
          title: this.$t('卡号'),
          align: 'center',
          dataIndex: 'chipNo',
        },
        {
          title: this.$t('姓名'),
          align: 'center',
          dataIndex: 'memberName',
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/member/bisMemberLottery/list',
        exportXlsUrl: '/member/bisMemberLottery/exportXls',
        importExcelUrl: 'member/bisMemberLottery/importExcel',
      },
      dictOptions: {},
      stateList: [
        {
          value: '',
          title: this.$t('全部'),
        },
      ],
      deviceList: [],
      totalCallback: this.totalCallbacks,
      totalInfo:{value: 0},
      queryParam: {
        tenantId: ''
      },
      shops:[],
      tenantid: Vue.ls.get('TENANT_ID'),
    }
  },
  async created() {
    if (!this.tenantid) {
      await initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
      let dtl = {
        title: this.$t('门店编码'),
        align: 'center',
        dataIndex: 'tenantId',
        width: 100,
        customCell: (r, i) => {
          return {
            style: {
              whiteSpace: 'normal',
            },
          }
        },
      },
        dtl1 = {
          title: this.$t('门店名称'),
          align: 'center',
          dataIndex: 'tenantId_dictText',
          width: 140,
          customRender: (text, record) => {
            if (text) {
              return text
            }
            let x = this.shops.findIndex(s => { return s.value == record.tenantId })
            if (x > -1) {
              return this.shops[x].title
            }
            return record.tenantId
          },
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        }
      this.columns.unshift(dtl1)
      this.columns.unshift(dtl)
    }
    this.getSuperFieldList();
    this.initDictConfig()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    handleSearch(value) {
      let query = {}
      if (value) {
        query.deviceInfo = value
      }
      getAction('/device/bisDeviceInfo/list', query).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
          this.deviceList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    initDictConfig() {
      initDictOptions('bis_member_lottery.state').then((res) => {
        if (res.success) {
          this.stateList = [
            {
              value: '',
              title: this.$t('全部'),
            },
          ]
          res.result.forEach((col) => {
            this.stateList.push(col)
          })
        }
      })
    },
    searchReset() {
      this.queryParam = {
        state: '0',
        lotteryType: 1,
        tenantId: ''
      }
      this.loadData(1)
    },
    handleFill(record) {
      this.$refs.fillModal.show(record)
    },
    handleCancel(record) {
      let query = {}
      query = Object.assign({}, record)
      query.state = '4'
      putAction('/member/bisMemberLottery/edit', query).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData(1)
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    setCheck(){
      this.$refs.checkModal.show()
    },
    totalCallbacks(totalInfo) {
      this.totalInfo = totalInfo
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>